package com.company.editortest;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Selection;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

public class EditorTest extends Activity {
	
	LinearLayout layout, search_and_navigation;
	EditText search_edit;
	NewEditText editor;
	Button search;
	Context c = this;
	InputMethodManager imm;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        
        search_and_navigation = (LinearLayout) findViewById(R.id.search_and_navigation);
        search_edit = (EditText) findViewById(R.id.search_edit);
        
        editor = new NewEditText(getApplication(), search_and_navigation, search_edit, imm);
        editor.setGravity(Gravity.TOP & Gravity.RIGHT);       
        editor.setText(R.string.test_string);
                
        
        layout = (LinearLayout) findViewById(R.id.editor);
        layout.addView(editor, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
                
        search = (Button) findViewById(R.id.sbutton);                         
        search.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(search_edit.length() == 0){
					Toast toast = Toast.makeText(getApplicationContext(), "Please enter some text to search", Toast.LENGTH_SHORT);
					toast.setGravity(Gravity.TOP, 0, 100);
					toast.show();
				}else{
					String searchString = search_edit.getText().toString();
					int start = editor.getText().toString().indexOf(searchString);
					int count = searchString.length();
					Selection.setSelection(editor.getText(), start, start+count);
					Log.d("start", Integer.toString(start));				
					Log.d("count", Integer.toString(count));
					editor.requestFocus();
				}
			}
		});
        
    }
}